﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimeToGo.Repositories;

namespace TimeToGo.Infrastructure
{
    internal class UnitOfWork<T>
    {
        private IRepository<T> _repository;

        public UnitOfWork(IRepository<T> repository)
        {
            _repository = repository;

            Added = new List<T>();
            Removed = new List<T>();
            Modified = new List<T>();
        }

        public List<T> Added { get; private set; }
        public List<T> Removed { get; private set; }
        public List<T> Modified { get; private set; }

        public void Add(T entity)
        {
            Added.Add(entity);
        }

        public void Remove(T entity)
        {
            Removed.Add(entity);
        }

        public void Modify(T entity)
        {
            Modified.Add(entity);
        }

        public void Process()
        {
            foreach (T entity in Modified)
                _repository.Modify(entity);

            foreach (T entity in Removed)
                _repository.Remove(entity);

            foreach (T entity in Added)
                _repository.Add(entity);
        }
    }
}
